/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.plugin;

import java.util.Properties;

/**
 * Mybatis插件的顶层接口，
 * 凡自定义Mybatis插件都需要实现这个接口.
 *
 * @date: 2019-07-04 11:47
 */
public interface Interceptor {

  /**
   * 定义插件的核心方法，该方法会覆盖被拦截对象原有的方法
   * 在该方法中实现自己的逻辑
   *
   * 通过Invocation获取目标对象，进一步取得 MetaObject，只要取到 MetaObject，
   * 我们就可以修改目标对象相关的属性值了。
   *
   * 比如，拦截StatementHandler，获取预编译之前的SQL语句:
   *    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
   *    MetaObject metaObjectHandler = SystemMetaObject.forObject(statementHandler);
   *    String sql = (String)metaObjectHandler.getValue("delegate.boundSql.sql");
   *
   * @param invocation
   * @return
   * @throws Throwable
   */
  Object intercept(Invocation invocation) throws Throwable;

  /**
   * 为被拦截的对象生成一个代理对象.
   * 通常使用Mybatis提供的Plugin类的静态方法--wrap 就可生成一个代理对象，实现的原理为：JDK动态代理
   *
   * @param target
   * @return
   */
  Object plugin(Object target);

  /**
   * mybatis配置文件中，可在plugin元素中配置参数，该方法在插件初始化的时候被调用时，
   * 便将参数值保持到Properties对象中.
   *
   * @param properties
   */
  void setProperties(Properties properties);

}
